package com.macro.mall.tiny.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macro.mall.tiny.modules.pms.mapper.ProductSkuAttrValueMapper;
import com.macro.mall.tiny.modules.pms.model.ProductSkuAttrValue;
import com.macro.mall.tiny.modules.pms.service.ProductSkuAttrValueService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductSkuAttrValueServiceImpl extends ServiceImpl<ProductSkuAttrValueMapper, ProductSkuAttrValue> implements ProductSkuAttrValueService {

    @Override
    public List<ProductSkuAttrValue> listBySkuId(Integer skuId) {
        LambdaQueryWrapper<ProductSkuAttrValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductSkuAttrValue::getSkuId, skuId)
                .eq(ProductSkuAttrValue::getDelFlag, 0);
        return list(wrapper);
    }

    @Override
    public boolean saveBatchBySkuId(Integer skuId, List<ProductSkuAttrValue> attrValueList) {
        // 先删除原有的属性值
        removeBySkuId(skuId);

        // 设置SKU ID和默认值
        for (ProductSkuAttrValue attrValue : attrValueList) {
            attrValue.setSkuId(skuId);
            attrValue.setDelFlag(0);
        }

        // 批量保存新的属性值
        return saveBatch(attrValueList);
    }

    @Override
    public boolean removeBySkuId(Integer skuId) {
        LambdaQueryWrapper<ProductSkuAttrValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductSkuAttrValue::getSkuId, skuId);

        List<ProductSkuAttrValue> attrValueList = list(wrapper);
        for (ProductSkuAttrValue attrValue : attrValueList) {
            attrValue.setDelFlag(1);
        }

        return updateBatchById(attrValueList);
    }
}